package swa08.client;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Properties;
import java.util.Vector;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import swa08.provider.Service;
import swa2012.telco.broker.beans.BrokerSendMessageStatus;
import swa2012.telco.broker.beans.Client;
import swa2012.telco.broker.beans.Message;
import swa2012.telco.broker.beans.Provider;

public class CustomClient
{
	private static final String providerEndpoint = "http://student.swa.univie.ac.at:8008/Provider/provider";
	//private static final String providerEndpoint = "http://localhost:8083/Provider/provider";
	private Service provider;
	private Client client;
	
	/*public CustomClient(String baseDir) throws Exception
	{
		File config = new File(baseDir + "/config.properties");
		Properties prop = new Properties();

		if(config.exists())
			prop.load(new FileInputStream(config));

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(Service.class);
        factory.setAddress(CustomClient.providerEndpoint);
        this.provider = (Service) factory.create();
    }*/
	
	public CustomClient(String id)
	{		
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(Service.class);
        factory.setAddress(CustomClient.providerEndpoint);
        this.provider = (Service) factory.create();
        
        this.client = this.provider.connectClient(id);
        System.out.println("Connected: " + this.provider);
	}
	
	public StringBuffer listClients()
	{
		StringBuffer out = new StringBuffer();
		out.append("Listing all clients connected to " + this.provider + "\n-----\n");
		Vector<Client> clients =  this.provider.listClient();
		
		if (clients == null)
		{
			out.append("No clients connected!\n");
		}
		else
		{
			for (Client c: clients)
			{
				out.append(c.toString() + "\n");
			}
		}
		
		out.append("-----\n");
		return out;
	}
	
	public boolean sendMessage(String text, String id, String providerID, String providerEndpoint)
	{
        Provider p;
		try
		{
			p = new Provider(providerID, new URI(providerEndpoint));
		}
		catch (URISyntaxException e)
		{
			e.printStackTrace();
			return false;
		}
        
		Client receiver = new Client(id, p);
		Message msg = new Message(this.client, receiver, text, System.currentTimeMillis());
		BrokerSendMessageStatus status = this.provider.sendMessage(msg);
		if (status.equals(BrokerSendMessageStatus.SEND_MESSAGE_ERROR))
		{
			return false;
		}
		else
		{
			System.out.println("sendMessage ('" + msg.getText() + "', '" + msg.getSender() + "', '" + msg.getReceiver() + "'): " + status);
			return true;
		}
	}
	
	public StringBuffer pullMessages()
	{
		StringBuffer out = new StringBuffer();
		out.append("Listing all messages for client " + this.client + "\n-----\n");
		Vector<Message> messages = this.provider.pullMessage(this.client);
		
		if (messages == null)
		{
			out.append("No messages stored!\n");
		}
		else
		{
			for (Message m: messages)
			{
				out.append(m.toString() + ": " + m.getText() + "\n");
			}
		}
		
		out.append("-----\n");
		return out;
	}
	
	public void destroy()
	{
		if (this.provider != null)
		{
			if(this.provider.disconnectClient(this.client))
			{
				System.out.println("Successfully disconnected!");
				this.provider = null;
			}
			else
			{
				System.err.println("Disconnect failed!");
			}
		}
	}
	
	protected void finalize()
	{
		destroy();
	}
}
